feat(controls): Make Durations customizable#1679
Conversation
|
@pomianowski yo! do you think this feature should be added? if yes, i was thinking about this and i think just 3 static TimeSpan properties should be fine instead of AnimationDurationExtension.cs |
| /// <summary> | ||
| /// A longer duration intended for prominent or decorative transitions. | ||
| /// </summary> | ||
| SlowDuration, |
There was a problem hiding this comment.
Let's drop the Duration from all, it's already an AnimationDuration.
I also think we can also structure this differently to avoid the Dictionary and ControlsDictionary changes.
Something like
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.
using System.Windows.Markup;
namespace Wpf.Ui.Animations;
/// <summary>
/// A XAML markup extension that resolves an <see cref="AnimationDuration" /> key
/// to a concrete <see cref="Duration" /> value at parse time.
/// This allows Storyboard animations to reference configurable durations
/// without requiring <c>DynamicResource</c> (which cannot be frozen).
/// </summary>
/// <example>
/// <code lang="xml">
/// <!-- Positional syntax -->
/// Duration="{markup:AnimationDuration Slow}"
///
/// <!-- Named property syntax -->
/// Duration="{markup:AnimationDuration Duration=Slow}"
/// </code>
/// </example>
public class AnimationDurationExtension : MarkupExtension
{
/// <summary>
/// Initializes a new instance of the <see cref="AnimationDurationExtension" /> class.
/// </summary>
public AnimationDurationExtension() { }
/// <summary>
/// Initializes a new instance of the <see cref="AnimationDurationExtension" /> class.
/// </summary>
public AnimationDurationExtension(AnimationDurationType animationDurationType)
{
Duration = AnimationDuration.FromType(animationDurationType);
}
/// <summary>
/// Gets or sets the <see cref="AnimationDuration" /> key to resolve.
/// </summary>
public AnimationDuration Duration { get; set; }
/// <summary>
/// Returns the <see cref="System.Windows.Duration" /> associated with the current
/// <see cref="Duration" /> key, including any user-defined override.
/// </summary>
public override object ProvideValue(IServiceProvider serviceProvider)
{
return Duration.Duration;
}
}
public struct AnimationDuration
{
private static readonly AnimationDuration[] _durations =
[
new() { Duration = TimeSpan.FromMilliseconds(333), Type = AnimationDurationType.Slow },
new() { Duration = TimeSpan.FromMilliseconds(167), Type = AnimationDurationType.Normal },
new() { Duration = TimeSpan.FromMilliseconds(80), Type = AnimationDurationType.Fast }
];
public Duration Duration { get; set; }
public AnimationDurationType Type { get; set; }
public static AnimationDuration Normal
{
get => _durations[1];
set => _durations[1] = value;
}
public static AnimationDuration Fast
{
get => _durations[2];
set => _durations[2] = value;
}
public static AnimationDuration Slow
{
get => _durations[0];
set => _durations[0] = value;
}
public static AnimationDuration FromType(AnimationDurationType type) => _durations[(int)type];
}
/// <summary>
/// Defines the duration types available for control animations.
/// </summary>
public enum AnimationDurationType
{
/// <summary>
/// A longer duration intended for prominent or decorative transitions.
/// </summary>
Slow,
/// <summary>
/// The standard duration for most interactive feedback animations.
/// </summary>
Normal,
/// <summary>
/// A shorter duration for rapid state changes.
/// </summary>
Fast,
}There was a problem hiding this comment.
sorry i dont really understand this comment,
how is "avoid Dictionary and ControlsDictionary changes." achieved?
There was a problem hiding this comment.
With the above code we don't have to make changes to Dictionary and ControlsDictionary.
There was a problem hiding this comment.
sorry my dumb question: how do I as a user define these durations in my app.xaml?
There was a problem hiding this comment.
Just change them through code. For example, AnimationDuration.Normal = new (xxx).
Durations on Animations are freezable, this pull request provides a way to make Durations customizable.
Pull request type
Please check the type of change your PR introduces:
What is the current behavior?
Durations on Animation has hard coded values for all controls
Issue Number: #1586
fixes #1586
What is the new behavior?
Durations can be of three types: SlowDuration, NormalDuration and FastDuration. These can all be set when merging ControlsDictionary. Defaults to previous hardcoded values (no breaking change)
<ui:ControlsDictionary SlowDuration="0:0:0.0" NormalDuration="0:0:0.0" FastDuration="0:0:0.0" />Other information